/**
 * Provide CRUD operations
 * under Item entity
 * @param baseUrl - host of your server
 * @param serviceUrl - relative url to your service
 * @constructor
 */

function ItemAjaxService(baseUrl, serviceUrl){
    this.opts = ajax.optionsCreate(baseUrl, null, serviceUrl);
}


/**
 *  Get item by id
 *  @param {Number} id
 *  @param {Function} callback
 */

ItemAjaxService.prototype.getItemById = function(id,callback){
    this.opts.params = this.opts.params + "?" + id
    ajax.get(this.opts, callback);
};

/**
 *  Get items which contains query string in their names
 *  @param {String} query
 *  @param {Function} callback
 */

ItemAjaxService.prototype.queryItemsByName = function(query,callback){
    this.opts.params = this.opts.params + "?" + encodeURI(query);
    ajax.get(this.opts, callback);
};

/**
 * Create items provided in items array on the server,
 * returns array of items ids to callback
 * @param {Array} items
 * @param {Function} callback
 */

ItemAjaxService.prototype.create = function(items, callback){
    for (var i = 0; i < items.length; i++) {
        this.opts.data = items[i];
        ajax.create(this.opts, callback);
    }
};

/**
 * Update items,
 * returns updated items id to callback
 * @param {Array} items
 * @param {Function} callback
 */

ItemAjaxService.prototype.update = function(items, callback){
    for (var i = 0; i < items.length; i++) {
        this.opts.data = items[i];
        ajax.update(this.opts, callback);
    }
};

/**
 * Delete items on the server
 * @param {Array} ids  - items ids
 * @param {Function} callback
 */

ItemAjaxService.prototype.remove = function(ids, callback){
    for (var i = 0; i < ids.length; i++) {
        this.opts.data = ids[i];
        ajax.remove(this.opts, callback);
    }
};
